//
//  addTodoView.swift
//  todoList
//
//  Created by Aoyun on 02/01/2025.
//

import SwiftUI

struct addTodoView: View {
    @Environment(\.dismiss) private var dismiss
    @Environment(ListViewModel.self) private var listViewModel
    @State private var toDo: String = ""
    @State private var ishow: Bool = false
    @State private var showText: String = ""
    var body: some View {
        VStack {
            TextField("write Todo", text: $toDo)
                .frame(maxWidth: .infinity)
                .frame(height: 30)
                .padding()
                .background(Color(uiColor: .secondarySystemBackground))
                .cornerRadius(15)
                .foregroundColor(.white)
            Button(action: {
                getTodo()
            }, label: {
                Text("save")
            }).frame(maxWidth: .infinity)
                .frame(height: 30)
                .padding()
                .background(.black)
                .foregroundColor(.white)
                .cornerRadius(15)
            Spacer()
        }.navigationTitle("Add Todo 🖊️").padding()
            .alert(showText, isPresented: $ishow, actions: {})
    }

    func getTodo() {
        guard toDo.count > 3 else{
            ishow.toggle()
            showText = "Write a todo that is longer than 3 characters"
            return
        }
        listViewModel.addList(item: toDo)
        dismiss()
    }
}

#Preview {
    NavigationStack {
        addTodoView().environment(ListViewModel())
    }
}
